package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CROSS_TRADE_WAR_WANTED_RANK, desc = "跨服商战通缉榜")
public class CrossTradeWarWantedRankCmd extends AbstractCommand {
    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossTradeWarProto.CrossTradeWarWantedRankReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarWantedRankReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
        if(null == activityInfo){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        //发送到跨服
        CrossTradeWarProto.CrossTradeWarWantedRankReqMsg.Builder reqMsg = CrossTradeWarProto.CrossTradeWarWantedRankReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);

        player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_TRADE_WAR_WANTED_RANK, reqMsg, activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret) {
            CrossTradeWarProto.CrossTradeWarWantedRankRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarWantedRankRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CROSS_TRADE_WAR_WANTED_RANK, respMsg);
        }
    }
}
